﻿Public Class SettingsForm

    Private Sub Load_Rules()
        Dim sql As String

        sql = "select cast(null as image) as Type, LR.Status, LR.Lock_ID as ID, LR.Lock_Description as Description, LR.Period_ID, " & _
              "periods.Period_Name as Period, LR.Region_Code, Regions.Region_Name as Region, " & _
              "LR.LA_Code, LA.LA_Name as 'Local Authority', LR.UPIN, Providers.Provider_Name as Provider, " & _
              "LR.Field_Code, Fields.Field_Description as Field, " & _
              "LR.Start_Date as 'Start Date', LR.End_Date as 'End Date' " & _
              "from Lock_Rules LR, " & _
              "(select Period_ID, Period_Name from Periods) periods, " & _
              "(select Region_Code, Region_Name from Regions " & _
              "union select '<All>', '<All>' from global_settings_upgrade) Regions, " & _
              "(select LA_Code, LA_Name from Local_Authorities " & _
              "union select '<All>', '<All>' from global_settings_upgrade) LA, " & _
              "(select UPIN, Provider_Name from Providers " & _
              "union select 0, '<All>' from global_settings_upgrade) Providers, " & _
              "(select Field_Code, Field_Description from dbo.Fields_List_Part " & _
              "union select 0, '<All>' from global_settings_upgrade) Fields " & _
              "where periods.Period_ID = LR.Period_ID " & _
              "and Regions.Region_Code = LR.Region_Code " & _
              "and LA.LA_Code = LR.LA_Code " & _
              "and Providers.UPIN = LR.UPIN " & _
              "and Fields.Field_Code = LR.Field_Code"

        LockGrid.DataSource = LoadDataView(sql)

        LockGrid.Columns("Status").Visible = False
        LockGrid.Columns("Period_ID").Visible = False
        LockGrid.Columns("Region_Code").Visible = False
        LockGrid.Columns("LA_Code").Visible = False
        LockGrid.Columns("Field_Code").Visible = False

        ' LockGrid.Columns.Add(New DataGridViewImageColumn)
        'LockGrid.Columns(LockGrid.ColumnCount - 1).Name = "Type"

    End Sub

    Private Sub Load_Close_Settings()
        Dim TheView As DataView
        Dim sql As String

        sql = "Select * from Close_Store"

        TheView = LoadDataView(sql)

        If TheView(0).Item("Close_Store") = 1 Then
            CloseStatusLabel.Text = "The Store is currently closed"
            CloseButton.Text = "Open The Store"
            CloseImage.Image = SettingsVisuals.Images(0)

        Else
            CloseStatusLabel.Text = "The Store is currently open"
            CloseButton.Text = "Close The Store"
            CloseImage.Image = SettingsVisuals.Images(1)
        End If

        TheMessageBox.Text = TheView(0).Item("Message").ToString

    End Sub

    Private Sub SettingsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Close_Settings()
        Load_Rules()

    End Sub

    Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to close the Store." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Close the Store", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        Dim sql As String

        If CloseButton.Text = "Close The Store" Then
            If MessageBox.Show("This will close The Store, are you sure you want to continue?", "Close The Store?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                sql = "Update Close_Store set Close_Store = 1, Message = '" & TheMessageBox.Text & "'"
                RunNonQuery(sql)
            End If
        Else
            If MessageBox.Show("This will open The Store, are you sure you want to continue?", "Open The Store?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                sql = "Update Close_Store set Close_Store = 0, Message = '" & TheMessageBox.Text & "'"
                RunNonQuery(sql)
            End If
        End If

        Load_Close_Settings()
    End Sub

    Private Sub AddBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddBut.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to add lock rules." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Add Lock Rule", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        Dim sql As String
        Dim StartDate As String
        Dim EndDate As String
        Dim Status As Integer

        'set default settings in form

        With LockRulesDialog
            .LoadPeriodBox()
            .LoadRegionBox()
            .LoadLABox()
            .LoadProviderBox()
            .LoadFieldBox()
            .DatesCheckBox.Checked = False
            .FromBox.Value = Now
            .ToBox.Value = Now
            .UnlockRadio.Checked = True
            .IDBox.Text = ""
            .DescriptionBox.Text = ""
            .IDBox.Enabled = True

            .IDBox.Focus()


            If LockRulesDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                If .UnlockRadio.Checked = True Then
                    Status = 0
                Else
                    Status = 1
                End If

                If .DatesCheckBox.Checked = True Then
                    StartDate = Fix(.FromBox.Value.ToOADate - 2)
                    EndDate = Fix(.ToBox.Value.ToOADate - 2)
                Else
                    StartDate = "Null"
                    EndDate = "Null"
                End If

                sql = "insert into Lock_Rules values('" & .IDBox.Text & "', '" & .DescriptionBox.Text & "'," & _
                      "'" & .PeriodBox.SelectedValue.ToString & "', '" & .RegionBox.SelectedValue.ToString & "'," & _
                      "'" & .LABox.SelectedValue.ToString & "', " & .ProviderBox.SelectedValue.ToString & "," & _
                      .FieldBox.SelectedValue.ToString & "," & StartDate & "," & EndDate & "," & Status & ")"

                'MessageBox.Show(sql)

                RunNonQuery(sql)
                Load_Rules()
            End If
        End With

    End Sub


    Private Sub LockGrid_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles LockGrid.CellFormatting
        If e.RowIndex < 0 Then Exit Sub

        If LockGrid.Columns(e.ColumnIndex).Name.Equals("Type") Then

            e.FormattingApplied = True

            Select Case LockGrid.Rows(e.RowIndex).Cells("Status").Value
                Case 0
                    e.Value = SettingsImages.Images(2)
                Case 1
                    e.Value = SettingsImages.Images(1)
            End Select

        End If
    End Sub

    Private Sub RefreshBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshBut.Click
        Load_Rules()

    End Sub

    Private Sub EditBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditBut.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to edit lock rules." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Edit Lock Rule", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        Dim sql As String
        Dim StartDate As String
        Dim EndDate As String
        Dim Status As Integer

        'set default settings in form
        If LockGrid.SelectedRows.Count = 0 Then Exit Sub

        With LockRulesDialog
            .LoadPeriodBox()
            .LoadRegionBox()
            .LoadLABox()
            .LoadProviderBox()
            .LoadFieldBox()

            If LockGrid.Item(LockGrid.Columns("Start Date").Index, LockGrid.CurrentRow.Index).Value.ToString = "" Then
                .DatesCheckBox.Checked = False
                .FromBox.Value = Now
                .ToBox.Value = Now
            Else
                .DatesCheckBox.Checked = True
                .FromBox.Value = LockGrid.Item(LockGrid.Columns("Start Date").Index, LockGrid.CurrentRow.Index).Value
                .ToBox.Value = LockGrid.Item(LockGrid.Columns("End Date").Index, LockGrid.CurrentRow.Index).Value
            End If

            If LockGrid.Item(LockGrid.Columns("Status").Index, LockGrid.CurrentRow.Index).Value.ToString = "1" Then
                .LockRadio.Checked = True
            Else
                .UnlockRadio.Checked = True
            End If

            .IDBox.Text = LockGrid.Item(LockGrid.Columns("ID").Index, LockGrid.CurrentRow.Index).Value.ToString
            .DescriptionBox.Text = LockGrid.Item(LockGrid.Columns("Description").Index, LockGrid.CurrentRow.Index).Value.ToString

            .PeriodBox.SelectedValue = LockGrid.Item(LockGrid.Columns("Period_ID").Index, LockGrid.CurrentRow.Index).Value
            .RegionBox.SelectedValue = LockGrid.Item(LockGrid.Columns("Region_Code").Index, LockGrid.CurrentRow.Index).Value
            .LABox.SelectedValue = LockGrid.Item(LockGrid.Columns("LA_Code").Index, LockGrid.CurrentRow.Index).Value
            .ProviderBox.SelectedValue = LockGrid.Item(LockGrid.Columns("UPIN").Index, LockGrid.CurrentRow.Index).Value
            .FieldBox.SelectedValue = LockGrid.Item(LockGrid.Columns("Field_Code").Index, LockGrid.CurrentRow.Index).Value

            .IDBox.Enabled = False
            .DescriptionBox.Focus()


            If LockRulesDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                If .UnlockRadio.Checked = True Then
                    Status = 0
                Else
                    Status = 1
                End If

                If .DatesCheckBox.Checked = True Then
                    StartDate = Fix(.FromBox.Value.ToOADate - 2)
                    EndDate = Fix(.ToBox.Value.ToOADate - 2)
                Else
                    StartDate = "Null"
                    EndDate = "Null"
                End If

                sql = "Update Lock_Rules set Lock_Description = '" & .DescriptionBox.Text & "'," & _
                      " Period_ID = " & .PeriodBox.SelectedValue.ToString & ", Region_Code = '" & .RegionBox.SelectedValue.ToString & "'," & _
                      " LA_Code = '" & .LABox.SelectedValue.ToString & "', UPIN = " & .ProviderBox.SelectedValue.ToString & ", Field_Code = " & _
                      .FieldBox.SelectedValue.ToString & ", Start_Date = " & StartDate & ", End_Date = " & EndDate & ", Status = " & Status & _
                      " where Lock_ID = '" & .IDBox.Text & "'"

                'MessageBox.Show(sql)

                RunNonQuery(sql)
                Load_Rules()
            End If
        End With
    End Sub

    Private Sub DeleteBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteBut.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to delete lock rules." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Delete Lock Rule", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        Dim sql As String

        If MessageBox.Show("Are you sure you want to delete the selected rule?", "Delete rule?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            sql = "delete Lock_Rules where Lock_ID = '" & LockGrid.Item(LockGrid.Columns("ID").Index, LockGrid.CurrentRow.Index).Value.ToString & "'"
            RunNonQuery(sql)
            Load_Rules()
        End If
    End Sub

    Private Sub SettingsForm_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        If UserFormCheck(Me.Name) = "N" Then
            Me.Close()
            MessageBox.Show("Currently you do not have permission to access this form." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk if you need access.", Me.Name & " Access", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
    End Sub
End Class